package util;

import java.io.File;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConfigUtil {
	
	private static final Log log = LogFactory.getLog(ConfigUtil.class);
	
	private static Configuration config;
	//加载配置文件
	private static final String  configName = "config.properties";
	
	static {
		try {
			if(isWindows()){
				config = new PropertiesConfiguration(configName);
			}else{
				String jarPath=JarToolUtil.getJarDir()+File.separator;
				config = new PropertiesConfiguration(jarPath+configName);
			}
		} catch (ConfigurationException e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
		}
	}

	public static String get(String key){
		return config.getString(key);
	}

	/**
	 * 根据属性名获得对应值，如果得不到值返回defaultValue
	 * 
	 * @param name 属性名称
	 * @param defaultValue 默认值
	 * @return 属性对应的值
	 */
	public static String get(String name, String defaultValue) {
		String ret = config.getString(name, defaultValue);
		if (ret == null) {
			return defaultValue;
		} else {
			return ret.trim();
		}
	}
	
	
	public static Integer getInteger(String name, Integer defaultValue) {
		Integer ret = config.getInteger(name, defaultValue);
		if (ret == null) {
			return defaultValue;
		} else {
			return ret;
		}
	}

	/**
	 * 判断当前操作系统是不是window
	 * 
	 * @return boolean
	 */
	public static boolean isWindows() {
		boolean flag = false;
		if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
			flag = true;
		}
		return flag;
	}
}
